package com.basic.library.binding.viewadapter.common

import android.view.View
import android.widget.TextView
import androidx.core.app.NotificationCompat.getVisibility
import androidx.databinding.BindingAdapter
import com.basic.library.R
import com.basic.library.base.getVisibility
import com.basic.library.base.twoPinJieC
import com.basic.library.binding.viewadapter.edittext.ViewAdapter
import com.basic.library.utils.EditInputFilter
import com.tzlibrary.customui.TwoSideTextView

@BindingAdapter(value = ["pinJieA"], requireAll = true)
fun setPinJieA(view: TextView, content: String?) {
    (view.hint ?: "").run {
        view.text = twoPinJieC(toString(), content, view.currentHintTextColor)
    }
}

@BindingAdapter(value = ["pinJieN"], requireAll = true)
fun setPinJieN(view: TextView, content: String?) {
    (view.hint ?: "").run {
        view.text = twoPinJieC(toString(), "\n" + (content ?: ""), view.currentHintTextColor)
    }
    view.visibility = getVisibility(!content.isNullOrEmpty())
}

@BindingAdapter(value = ["pinJieAVisibility"], requireAll = true)
fun setPinJieAVisibility(view: TextView, content: String?) {
    (view.hint ?: "").run {
        view.text = twoPinJieC(toString(), content, view.currentHintTextColor)
    }
    view.visibility = getVisibility(!content.isNullOrEmpty())
}

@BindingAdapter(value = ["pinJieA1"], requireAll = true)
fun setPinJieA1(view: TextView, content: String?) {
    (view.hint ?: "").run {
        view.text = twoPinJieC(toString(), content)
    }
}

@BindingAdapter(value = ["textVisibility"], requireAll = true)
fun setTextVis(view: TextView, content: String?) {
    view.text = content
    view.visibility = getVisibility(!content.isNullOrEmpty())
}

@BindingAdapter(value = ["contentVisibility"], requireAll = true)
fun setViewVis(view: View, content: String?) {
    view.visibility = getVisibility(!content.isNullOrEmpty())
}

@BindingAdapter(value = ["textTstVisibility"], requireAll = true)
fun setTextVis(view: TwoSideTextView, content: String?) {
    val editText = view.getRightEditText()
    editText.setText(content)
    view.visibility = getVisibility(!content.isNullOrEmpty())
}

@BindingAdapter(value = ["moneyLimitTst"], requireAll = false)
fun setValueLimit(tst: TwoSideTextView, max: String?) {
    val editText = tst.getRightEditText()
    if (max == null) return
    val key = editText.getTag(R.id.tagEditInputFilter)
    if (key != null) {
        val filter = ViewAdapter.map[key]
        filter?.setMAX_VALUE(max)
    } else {
        val random = "${System.currentTimeMillis() * Math.random()}"
        ViewAdapter.map[random] = EditInputFilter(editText, max, random)
    }
}